﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace printdesign
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            using (FileStream fs = new FileStream(
System.IO.Path.Combine(Environment.CurrentDirectory, "saved.xaml"),
FileMode.Create, FileAccess.Write))
            {
                XamlWriter.Save(mainCanvas, fs);//mainCanvas为你的Panel的Name
                fs.Close();
            }
        }

        private void OpenPrintTestwindow(object sender, RoutedEventArgs e)
        {
            WindowTestprint t = new WindowTestprint();
            t.Show();
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            //lable1.Tag = "name";
            //Binding binding = new Binding()
            //{
            //    Source=new Student(),
            //    Path = new PropertyPath("name")// 需绑定的数据源属性名
            //    ,
            //    Mode = BindingMode.TwoWay// 绑定模式
            //    ,
            //    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            //};

            //// 连接数据源与绑定目标
            //BindingOperations.SetBinding(
            //    btn1// 需绑定的控件
            //    ,
            //    TextBox.TextProperty// 需绑定的控件属性
            //    ,
            //    binding);

        }

        private void ADDRectangle_Click(object sender, RoutedEventArgs e)
        {
            Border btn = new Border() { Width=500,Height=360,BorderBrush=new SolidColorBrush(Colors.Black),BorderThickness=new Thickness(1)};
            btn.MouseLeftButtonDown += btn_MouseLeftButtonDown;
            btn.MouseMove += btn_MouseMove;
            btn.MouseLeftButtonUp += btn_MouseLeftButtonUp;
            mainCanvas.Children.Add(btn);
        }
        Point pos = new Point();
        void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Border tmp = (Border)sender;
            pos = e.GetPosition(null);
            tmp.CaptureMouse();
            tmp.Cursor = Cursors.Hand;
        }
        void btn_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Border tmp = (Border)sender;
                double dx = e.GetPosition(null).X - pos.X + tmp.Margin.Left;
                double dy = e.GetPosition(null).Y - pos.Y + tmp.Margin.Top;
                tmp.Margin = new Thickness(dx, dy, 0, 0);
                pos = e.GetPosition(null);
            }
        }
        void btn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Border tmp = (Border)sender;
            tmp.ReleaseMouseCapture();
        }
      
        void line_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Line tmp = (Line)sender;
            pos = e.GetPosition(null);
            tmp.CaptureMouse();
            tmp.Cursor = Cursors.Hand;
        }
        void line_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Line tmp = (Line)sender;
                double dx = e.GetPosition(null).X - pos.X + tmp.Margin.Left;
                double dy = e.GetPosition(null).Y - pos.Y + tmp.Margin.Top;
                tmp.Margin = new Thickness(dx, dy, 0, 0);
                pos = e.GetPosition(null);
            }
        }
        void line_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Line tmp = (Line)sender;
            tmp.ReleaseMouseCapture();
        }

        private void ADDHorizontalLine_Click(object sender, RoutedEventArgs e)
        {
            Line line = new Line() {  Stroke=new SolidColorBrush(Colors.Red), X1=0,Y1=10,X2=500,Y2=10};
            line.MouseLeftButtonDown += line_MouseLeftButtonDown;
            line.MouseMove += line_MouseMove;
            line.MouseLeftButtonUp += line_MouseLeftButtonUp;
            mainCanvas.Children.Add(line);
        }

        private void ADDtextblock_Click(object sender, RoutedEventArgs e)
        {
            AddedTextBlock tb = new AddedTextBlock(this);
            mainCanvas.Children.Add(tb);
        }
    }
    public class Student
    {
        public string name { get; set; }
        public string age { get; set; }
    }
}
